package com.ssm.equip.server.controller.lock;


import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.equip.dao.model.Lock;
import com.ssm.equip.rpc.api.lock.LockService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地锁管理 Controller
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/lock")
public class LockController  {


    /**
     * 进入地锁添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("lock:add")
    public String add(ModelMap model, HttpSession session){
       Account account= (Account) session.getAttribute(Const.ACCOUNT);
        model.put("longId", UUIDFactory.getStringId());
        model.put("type", account.getType());
        return "manager/lock/add";
    }


    /**
     * 进入地锁编辑页面
     * @param model 返回地锁的容器
     * @param id 地锁id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("lock:edit")
    public String edit(ModelMap model, @RequestParam String id, HttpSession session) {
        Lock lock=lockService.selectById(id);
       /* lock.setParking(parkingService.selectById(lock.getParkingId()));*/
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        model.put("type",account.getType());
        model.put("data",lock);
        return "manager/lock/edit";
    }


    /**
     * 地锁添加
     * 2017-5-26 10:06:24 韩榕榕
     * @param lock 带id的地锁对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("lock:add")
    @AdminControllerLog(description = "添加地锁" )
    public Tip save(Lock lock)  {
        try {
            if(lockService.countNumber(lock)>0){
                return  new Tip(2,"地锁编号已存在");
            }
            lockService.insert(lock);
            return new Tip();
        } catch (Exception e) {
             e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改地锁
     * 2017-5-26 10:17:30 韩榕榕
     * @param lock 带id的地锁对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("lock:edit")
    @AdminControllerLog(description = "修改地锁" )
    public Tip update(Lock lock,@RequestParam String id) {

        try {
            if(!lockService.selectById(id).getNumber().equals(lock.getNumber())){
                if(lockService.countNumber(lock)>0){
                   return new Tip(2,"地锁编号已存在");
                }
            }
            lockService.updateById(lock);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除地锁
     * @param id 地锁id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("lock:delete")
    @AdminControllerLog(description = "删除地锁" )
    public Tip delete(@RequestParam String id) {
        try {

            lockService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
             e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 地锁id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("lock:delete")
    @AdminControllerLog(description = "批量删除地锁" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            lockService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
             e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找地锁
     * @param id 地锁id
     * @return 地锁对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("lock:list")
    public Lock find(@RequestParam String id) {

        return lockService.selectById(id);
    }


    /**
     * 获取地锁列表 获取全部 不分页
     * @param request 请求参数
     * @return 地锁列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("lock:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取地锁列表 分页
     * 2017-5-24 15:43:19 韩榕榕
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 地锁列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("lock:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session=request.getSession();
       Account account= (Account) session.getAttribute(Const.ACCOUNT);
       ParamMap paramMap=new ParamMap(request);
        if(account.getType()==6){
            paramMap.put("parkingId",account.getInfoId());
        }else if(account.getType()==8){
            paramMap.put("officeId",account.getInfoId());
        }
/*        modelMap.put("parking",parkingService.listAll(null));*/
        modelMap.put("type",account.getType());
        modelMap.put("pageInfo",lockService.list(paramMap,page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/lock/list";
    }

    /**
     * 修改地锁的状态为打开
     * @param id
     * @return
     */
    @RequestMapping(value="/updateIoOpen")
    @ResponseBody
    public Tip updateIoOpen( @RequestParam String id){
        try {
            Lock lock=lockService.selectById(id);
            lock.setLockIo("1");
            lockService.updateById(lock);
            return  new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"数据修改异常");
        }
    }

    /**
     * 修改地锁的状态为关闭
     * @param id
     * @return
     */
    @RequestMapping(value="/updateIoClose")
    @ResponseBody
    public Tip updateIoClose( @RequestParam String id){
        try {
            Lock lock=lockService.selectById(id);
            lock.setLockIo("0");
            lockService.updateById(lock);
            return  new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"数据修改异常");
        }
    }



    //---------------------------- property -------------------------------

    @Resource
    private LockService lockService;

    /*@Resource
    private ParkingService parkingService;*/

}
